﻿﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class DatabaseHelper
    {
        private static string sql = "SELECT * FROM  commodity";

        private static string constr = "server=.;database=Supermarket;uio=sa;pwd=1wangyuexin.;";

        public static void UserinfoReader()
        {

            string constr = "server=.;database=User1;uid=sa;pwd=wangyuexin;";//获取连接，连接数据库

            SqlConnection conn = new SqlConnection(constr);

            conn.Open();//指令对象 

            SqlCommand cmd = new SqlCommand(sql, conn);//执行命令

            SqlDataReader sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", "Id", "商品名称", "商品价格", "商品类型");
            }
            conn.Close();
        }
        public static void UserinfoAdapter()
        {
            SqlDataAdapter sqldataAdapter = new SqlDataAdapter(sql, constr);//接受指令

            DataTable dataTable = new DataTable();

            sqldataAdapter.Fill(dataTable);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("{0}\t{1}\t{2}\t{3}", "Id", "商品名称", "商品价格", "商品类型");

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", dataTable.Rows[i]["commodityId"], dataTable.Rows[i]["commodityName"], dataTable.Rows[i]["price"], dataTable.Rows[i]["commodityKind"]);

            }
        }
    }
}
